package com.zcyh.exam.interceptor;

import com.zcyh.exam.config.LoginContext;
import com.zcyh.exam.config.LoginInfo;
import com.zcyh.exam.constants.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.Set;

/**
 * 请求访问配置
 */

@Slf4j
@Component
public class AuthorizeInterceptor implements HandlerInterceptor {

    @Autowired
    private LoginContext loginContext;

    private static Set<String>  ALLOW_ACCESS_PATHS = new HashSet<>();
    static {
        ALLOW_ACCESS_PATHS.add("/doc.html");
        ALLOW_ACCESS_PATHS.add("/error");
    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LoginInfo loginInfo = new LoginInfo();
        String id = request.getHeader(CommonConstant.X_ACCESS_ID);
        String userName = request.getHeader(CommonConstant.X_ACCESS_USERNAME);
        loginInfo.setUserId(id);
        loginInfo.setUserName(userName);
        loginContext.setLoginInfo(loginInfo);
        log.info("登陆用户信息:{},{}",userName,id);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("after completion:");

    }




}
